package edu.univercidade.tcc.sap.ejb;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import edu.univercidade.tcc.sap.model.IssueCategory;

public class IssueCategoryService implements IssueCategoryServiceLocal {

	@PersistenceContext
	EntityManager em;

	@Override
	public IssueCategory create(IssueCategory category) {
		em.persist(category);
		return category;
	}

	@Override
	public void delete(IssueCategory category) {
		em.remove(em.getReference(IssueCategory.class, category.getId()));
	}

	@SuppressWarnings("unchecked")
	@Override
	public IssueCategory[] listValid() {
		Query q = em.createNamedQuery("categories");
		return ((List<IssueCategory>) q.getResultList())
				.toArray(new IssueCategory[] {});
	}

	@Override
	public void update(IssueCategory category) {
		em.merge(category);
	}
}
